package com.marchsoft.campushelp.rest;

import me.zhengjie.aop.log.Log;
import com.marchsoft.campushelp.domain.CampusExchangeForm;
import com.marchsoft.campushelp.service.CampusExchangeFormService;
import com.marchsoft.campushelp.service.dto.CampusExchangeFormQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Api(tags = "交易表管理")
@RestController
@RequestMapping("/api/campusExchangeForm")
public class CampusExchangeFormController {

    private final CampusExchangeFormService campusExchangeFormService;

    public CampusExchangeFormController(CampusExchangeFormService campusExchangeFormService) {
        this.campusExchangeFormService = campusExchangeFormService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('campusExchangeForm:list')")
    public void download(HttpServletResponse response, CampusExchangeFormQueryCriteria criteria) throws IOException {
        campusExchangeFormService.download(campusExchangeFormService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询交易表")
    @ApiOperation("查询交易表")
    @PreAuthorize("@el.check('campusExchangeForm:list')")
    public ResponseEntity<Object> getCampusExchangeForms(CampusExchangeFormQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(campusExchangeFormService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增交易表")
    @ApiOperation("新增交易表")
    @PreAuthorize("@el.check('campusExchangeForm:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CampusExchangeForm resources){
        return new ResponseEntity<>(campusExchangeFormService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改交易表")
    @ApiOperation("修改交易表")
    @PreAuthorize("@el.check('campusExchangeForm:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CampusExchangeForm resources){
        campusExchangeFormService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除交易表")
    @ApiOperation("删除交易表")
    @PreAuthorize("@el.check('campusExchangeForm:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        campusExchangeFormService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}